package com.jfzt.config;

import com.alibaba.fastjson2.JSONObject;
import com.jfzt.pojo.UserJsonVO;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;

/**
 * 自定义请求拦截器
 * @author: shunpeng.hu
 * @date: 2024/1/8 13:57
 */

public class HttpRequestInterceptior  implements HandlerInterceptor  {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("request.getLocalAddr() = " + request.getLocalAddr());
        System.out.println("request.getLocalPort() = " + request.getLocalPort());
        System.out.println("request.getMethod() = " + request.getMethod());
        System.out.println("request.getRequestURI() = " + request.getRequestURI());
        // 请求参数
        System.out.println("request.getQueryString() = " + request.getQueryString());
        // 被请求的 IP
        System.out.println("request.getRemotePort() = " + request.getRemotePort());
        // 被请求的 port
        System.out.println("request.getRemoteAddr() = " + request.getRemoteAddr());
        //被请求用户
        System.out.println("request.getRemoteUser() = " + request.getRemoteUser());

        StringBuilder sb = new StringBuilder();
        BufferedReader reader = null;
        String line;
        try {
            reader = request.getReader();
            while ((line = reader.readLine()) != null) {
                sb.append(line).append('\n');
            }
        } catch (Exception e) { /*report an error*/ }finally {
            if (reader!=null){
                reader.close();
                String requestBody = sb.toString();
                UserJsonVO kcRealmClient = JSONObject.parseObject(requestBody, UserJsonVO.class);
                System.out.println(kcRealmClient.getKcRealmClient().getKcAdmin());
                System.out.println(kcRealmClient.getKcRealmClient().getKcRealm());
                System.out.println(kcRealmClient.getKcRealmClient().getKcClient());
                System.out.println(kcRealmClient.getUserPayload().getUserId());
                System.out.println(kcRealmClient.getUserPayload().getGroupNames());
                System.out.println(kcRealmClient.getUserPayload().getUsername());
                System.out.println(kcRealmClient.getUserPayload().getEmail());
                System.out.println(kcRealmClient.getUserPayload().isStatus());
                System.out.println(kcRealmClient.getUserPayload().getNickname());
                System.out.println(kcRealmClient.getUserPayload().getLastName());
                System.out.println(kcRealmClient.getUserPayload().getFirstName());

            }

        }
        System.out.println("request.getAttribute(\"kcUri\") = " + request.getAttribute("kcUri"));
        System.out.println("request.getAttribute(\"requestDate\") = " + request.getAttribute("requestDate"));

        System.out.println("request.getAttribute(\"data\") = " + request.getAttribute("data"));
        System.out.println("request.getAttribute(\"responseBody\") = " + request.getAttribute("responseBody"));
        System.out.println("request.getAttribute(\"responseDate\") = " + request.getAttribute("responseDate"));


        System.out.println("response.getStatus() = " + response.getStatus());
        System.out.println("request.getAttribute(\"耗时\") = " + request.getAttribute("耗时"));
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }


}
